<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Denzel - es6</title>
    <meta name="description" content="前端笔记">
    <meta name="keywords" content="web前端,HTML5,CSS3,Javascript,SVG,canvas">
  <meta name="anthor" content="Denzel">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
  <meta name="msapplication-TileColor" content="#000000">
  <link rel="icon" href="/blog/vuepress/logo.png">
  <link rel="manifest" href="/blog/vuepress/manifest.json">
  <link rel="apple-touch-icon" href="/blog/vuepress/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="/blog/vuepress/icons/safari-pinned-tab.svg" color="#3eaf7c">
    
    <link rel="preload" href="/blog/vuepress/assets/css/0.styles.d4f29607.css" as="style"><link rel="preload" href="/blog/vuepress/assets/js/app.15770e34.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/11.4d115f98.js" as="script"><link rel="prefetch" href="/blog/vuepress/assets/js/46.59c0ece7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/1.05e4270f.js"><link rel="prefetch" href="/blog/vuepress/assets/js/2.ace739f7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/3.636e5a79.js"><link rel="prefetch" href="/blog/vuepress/assets/js/4.7a55aa4b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/5.b56b0573.js"><link rel="prefetch" href="/blog/vuepress/assets/js/6.e9a3fbc8.js"><link rel="prefetch" href="/blog/vuepress/assets/js/7.aa46c47d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/8.a9288ac6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/9.b9f8dec6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/10.3e2ea470.js"><link rel="prefetch" href="/blog/vuepress/assets/js/12.c97cf4d0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/13.1e0cf594.js"><link rel="prefetch" href="/blog/vuepress/assets/js/14.bab9026a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/15.1a24cffe.js"><link rel="prefetch" href="/blog/vuepress/assets/js/16.e9995850.js"><link rel="prefetch" href="/blog/vuepress/assets/js/17.86a1f7d2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/18.c32400c4.js"><link rel="prefetch" href="/blog/vuepress/assets/js/19.9f99887c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/20.82f0c2f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/21.7f5c223d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/22.acf9934c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/23.20d8e817.js"><link rel="prefetch" href="/blog/vuepress/assets/js/24.acf27379.js"><link rel="prefetch" href="/blog/vuepress/assets/js/25.20165510.js"><link rel="prefetch" href="/blog/vuepress/assets/js/26.83a2ddc3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/27.33276a04.js"><link rel="prefetch" href="/blog/vuepress/assets/js/28.e320efab.js"><link rel="prefetch" href="/blog/vuepress/assets/js/29.3fd92c30.js"><link rel="prefetch" href="/blog/vuepress/assets/js/30.2bc84d43.js"><link rel="prefetch" href="/blog/vuepress/assets/js/31.d70ed500.js"><link rel="prefetch" href="/blog/vuepress/assets/js/32.e7a0f8e1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/33.c48ee70b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/34.8ad03d1b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/35.5bdb3994.js"><link rel="prefetch" href="/blog/vuepress/assets/js/36.a25f926e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/37.7515cfbb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/38.99347371.js"><link rel="prefetch" href="/blog/vuepress/assets/js/39.3481fc62.js"><link rel="prefetch" href="/blog/vuepress/assets/js/40.bc709734.js"><link rel="prefetch" href="/blog/vuepress/assets/js/41.e1e0a3bf.js"><link rel="prefetch" href="/blog/vuepress/assets/js/42.762b61b0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/43.8f0b2d6c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/44.2a0a4ee0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/45.baf7ed60.js"><link rel="prefetch" href="/blog/vuepress/assets/js/47.11a8720a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/48.e0bf8b54.js"><link rel="prefetch" href="/blog/vuepress/assets/js/49.c96c4011.js"><link rel="prefetch" href="/blog/vuepress/assets/js/50.31189263.js"><link rel="prefetch" href="/blog/vuepress/assets/js/51.766a8294.js"><link rel="prefetch" href="/blog/vuepress/assets/js/52.26b971fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/53.0cfbd708.js"><link rel="prefetch" href="/blog/vuepress/assets/js/54.bdc15c87.js"><link rel="prefetch" href="/blog/vuepress/assets/js/55.fea285a9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/56.e030a6b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/57.bd168a1e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/58.636557fa.js"><link rel="prefetch" href="/blog/vuepress/assets/js/59.5d1d0ce6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/60.0b779f92.js"><link rel="prefetch" href="/blog/vuepress/assets/js/61.4ec3f2eb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/62.8f70f114.js"><link rel="prefetch" href="/blog/vuepress/assets/js/63.9ac2b61d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/64.c028fad5.js"><link rel="prefetch" href="/blog/vuepress/assets/js/65.923a8d15.js"><link rel="prefetch" href="/blog/vuepress/assets/js/66.73db652e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/67.1a91446d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/68.e5a12a42.js"><link rel="prefetch" href="/blog/vuepress/assets/js/69.6ddcb882.js"><link rel="prefetch" href="/blog/vuepress/assets/js/70.ceb70d85.js"><link rel="prefetch" href="/blog/vuepress/assets/js/71.a804ef40.js"><link rel="prefetch" href="/blog/vuepress/assets/js/72.f1c4098c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/73.2f28b842.js"><link rel="prefetch" href="/blog/vuepress/assets/js/74.571c2ab1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/75.f365c2a1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/76.0916a3cd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/77.469aed17.js"><link rel="prefetch" href="/blog/vuepress/assets/js/78.02cdd59d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/79.44d7f95c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/80.4ca072f1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/81.d9239a49.js"><link rel="prefetch" href="/blog/vuepress/assets/js/82.b09096b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/83.f4bc8a9b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/84.1ffe6969.js"><link rel="prefetch" href="/blog/vuepress/assets/js/85.44e980f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/86.c8579ac0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/87.cd49f324.js"><link rel="prefetch" href="/blog/vuepress/assets/js/88.3851b6ff.js"><link rel="prefetch" href="/blog/vuepress/assets/js/89.b05297be.js"><link rel="prefetch" href="/blog/vuepress/assets/js/90.c1e3263e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/91.b493a1bb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/92.80ab06fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/93.c6ca00b3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/94.e993d310.js"><link rel="prefetch" href="/blog/vuepress/assets/js/95.8e19a7ed.js">
    <link rel="stylesheet" href="/blog/vuepress/assets/css/0.styles.d4f29607.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><a href="/blog/vuepress/" class="home-link router-link-active"><img src="/blog/vuepress/hero.png" class="logo"><span class="site-name can-hide">
      Denzel
    </span></a><div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""><!----></div><nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link router-link-active">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header><div class="sidebar-mask"></div><div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link router-link-active">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav><ul class="sidebar-links"><li><div class="sidebar-group first collapsable"><p class="sidebar-heading open"><span>study</span><span class="arrow down"></span></p><ul class="sidebar-group-items"><li><a href="/blog/vuepress/study/" class="sidebar-link">标签</a></li><li><a href="/blog/vuepress/study/DOM.html" class="sidebar-link">DOM</a></li><li><a href="/blog/vuepress/study/js_A.html" class="sidebar-link">js A</a></li><li><a href="/blog/vuepress/study/js_AA.html" class="sidebar-link">js AA</a></li><li><a href="/blog/vuepress/study/js_AAA.html" class="sidebar-link">js AAA</a></li><li><a href="/blog/vuepress/study/css_A.html" class="sidebar-link">CSS A</a></li><li><a href="/blog/vuepress/study/css_AA.html" class="sidebar-link">CSS AA</a></li><li><a href="/blog/vuepress/study/css_AAA.html" class="sidebar-link">CSS AAA</a></li><li><a href="/blog/vuepress/study/performance.html" class="sidebar-link">性能</a></li><li><a href="/blog/vuepress/study/vue.html" class="sidebar-link">vue</a></li><li><a href="/blog/vuepress/study/vue_code.html" class="sidebar-link">vue code</a></li><li><a href="/blog/vuepress/study/algorithm.html" class="sidebar-link">算法</a></li><li><a href="/blog/vuepress/study/webpack.html" class="sidebar-link">webpack</a></li><li><a href="/blog/vuepress/study/es6.html" class="active sidebar-link">es6</a></li><li><a href="/blog/vuepress/study/questions.html" class="sidebar-link">问题</a></li><li><a href="/blog/vuepress/study/mobile.html" class="sidebar-link">移动端</a></li><li><a href="/blog/vuepress/study/http.html" class="sidebar-link">http</a></li><li><a href="/blog/vuepress/study/diy.html" class="sidebar-link">DIY系列</a></li><li><a href="/blog/vuepress/study/api.html" class="sidebar-link">API</a></li><li><a href="/blog/vuepress/study/browser.html" class="sidebar-link">浏览器原理</a></li><li><a href="/blog/vuepress/study/conception.html" class="sidebar-link">概念</a></li><li><a href="/blog/vuepress/study/engineering.html" class="sidebar-link">工程化</a></li></ul></div></li></ul></div><div class="page"><div class="content"><h1 id="es6"><a href="#es6" aria-hidden="true" class="header-anchor">#</a> es6</h1><h2 id="新特性总览"><a href="#新特性总览" aria-hidden="true" class="header-anchor">#</a> 新特性总览</h2><ul><li><ol><li>let const (变量声明)</li></ol></li><li><ol start="2"><li>解构赋值</li></ol></li><li><ol start="3"><li>Promise 对象</li></ol></li><li><ol start="4"><li>async / await (nodejs controller中经常使用，查询数据库)</li></ol></li><li><ol start="5"><li>箭头函数</li></ol></li><li><ol start="6"><li>模板字符串</li></ol></li><li><ol start="7"><li>字符扩展方法:</li></ol><ul><li><ol><li>查找 String.prototype.includes() startWith endsWith</li></ol></li></ul></li><li><ol start="8"><li>函数引入了: rest参数 fn(...param) &amp; 箭头函数</li></ol></li><li><ol start="9"><li>... 扩展运算符</li></ol></li><li><ol start="10"><li>数组扩展方法:</li></ol><ul><li><ol><li>查找：find() findIndex() includes()</li></ol></li><li><ol start="2"><li>遍历：keys() entries() values()</li></ol></li></ul></li><li><ol start="11"><li>对象扩展方法:</li></ol><ul><li><ol><li>Object.assign()</li></ol></li><li><ol start="2"><li>Object.keys() Object.entries() Object.setPrototypeOf() Object.values()</li></ol></li><li><ol start="3"><li>Object.getOwnPropertyDescriptors()</li></ol></li><li><ol start="4"><li>__proto__属性，Object.()，Object.getPrototypeOf() 设置对象的原型，非常重要</li></ol></li></ul></li><li><ol start="12"><li>循环：for...of</li></ol></li><li><ol start="13"><li>Class</li></ol></li><li><ol start="14"><li>Module</li></ol><ul><li><ol><li>import</li></ol></li><li><ol start="2"><li>export</li></ol></li></ul></li><li><ol start="15"><li>Number isFinite isNaN isInteger</li></ol></li><li><ol start="16"><li>Set Map 两种新的数据类型(数据结构)</li></ol><ul><li><ol><li>Set (去重的数组)</li></ol><ul><li><ol><li>size</li></ol></li><li><ol start="2"><li>has add delete clear()</li></ol></li><li><ol start="3"><li>遍历: keys values entries 返回的都是一个遍历器对象</li></ol></li></ul></li><li><ol start="2"><li>Map (键值类型没有限制的map)</li></ol><ul><li><ol><li>set get</li></ol></li><li><ol start="2"><li>has delete clear()</li></ol></li><li><ol start="3"><li>遍历: keys values entries 返回的都是一个遍历器对象</li></ol></li></ul></li></ul></li><li><ol start="17"><li>Iterator 和 for...of 循环 (引入了遍历器对象的概念,与之搭配的是新的for of 遍历方法)</li></ol></li></ul><h2 id="let-const"><a href="#let-const" aria-hidden="true" class="header-anchor">#</a> let const</h2><ul><li>let 用来声明变量，并且会在当前作用域形成代码块 TDZ(Temporal Dead Zone)暂时性死区</li><li>const 用来声明常量，所谓常量就是<strong>物理指针</strong>不可以更改的变量。即任何改变内存地址的操作都不允许。除此之外特性跟 let 一样。对于引用类型的数据，在使用时要注意。</li><li>const 声明的对象或数组可以改变，只要不改变其内存地址。不能做到冻结对象，要冻结对象还要使用Object.freeze();</li></ul><pre class="language-js"><code><span class="token keyword">var</span> <span class="token function-variable function">constantize</span> <span class="token operator">=</span> <span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  	Object<span class="token punctuation">.</span><span class="token function">freeze</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  	Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>key <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token string">'object'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      		<span class="token function">constantize</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token punctuation">}</span>
  	<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre><ul><li>let声明变量，变量不会提升</li></ul><h2 id="箭头函数"><a href="#箭头函数" aria-hidden="true" class="header-anchor">#</a> 箭头函数</h2><p>除了便捷的写法外，注意this的指向问题：</p><ul><li>默认绑定外层this</li></ul><pre class="language-js"><code><span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
	a<span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>    
<span class="token punctuation">}</span>
obj<span class="token punctuation">.</span><span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">//打出的是obj对象</span>
</code></pre><pre class="language-js"><code><span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
obj<span class="token punctuation">.</span><span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">//打出来的是window</span>
</code></pre><ul><li>不能用call方法修改里面的this</li></ul><pre class="language-js"><code><span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
obj<span class="token punctuation">.</span>a<span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string">'123'</span><span class="token punctuation">)</span>  <span class="token comment">//打出来的结果依然是window对象</span>
</code></pre><ul><li>箭头函数不会创建自己的this,它只会从自己的作用域链的上一层继承this。
箭头函数不使用自身的this，而是一层一层的向上查找，直到有this。</li></ul><pre class="language-js"><code><span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    b<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    	c<span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
obj<span class="token punctuation">.</span><span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// 打出的是obj对象, 相当于obj.a.call(obj)</span>
obj<span class="token punctuation">.</span>b<span class="token punctuation">.</span><span class="token function">c</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//打出的是obj.b对象, 相当于obj.b.c.call(obj.b)</span>
</code></pre><pre class="language-js"><code><span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    b<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    	c<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
obj<span class="token punctuation">.</span><span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span>   <span class="token comment">//没有使用箭头函数打出的是obj</span>
obj<span class="token punctuation">.</span>b<span class="token punctuation">.</span><span class="token function">c</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">//打出的是window对象！！</span>
</code></pre><h2 id="for-in、for-of、foreach"><a href="#for-in、for-of、foreach" aria-hidden="true" class="header-anchor">#</a> for in、for of、forEach</h2><ul><li>forEach 用来遍历数组，遍历的是索引，但是不能中断循环(break或return）</li><li>for in 可遍历数组和对象，遍历的是索引，但会把原型方法和可枚举属性全部遍历出来（可用hasOwnPropery筛选掉），且不能保证对象的内部顺序，不推荐用来遍历数组。</li><li>for of 用来遍历iterable类型的对象，遍历的是元素值，Array、Map和Set都属于iterable类型。(普通对象{a: '1', b: '2', c: '3', d: '4'}不是iterable，不可以遍历)</li></ul><h2 id="class"><a href="#class" aria-hidden="true" class="header-anchor">#</a> class</h2><p>基本框架：</p><pre class="language-js"><code>	<span class="token comment">//变量方法名 可计算成员名称</span>
	<span class="token keyword">let</span> funStr <span class="token operator">=</span> <span class="token string">'get'</span> <span class="token operator">+</span> <span class="token string">'Age'</span><span class="token punctuation">;</span>
	<span class="token keyword">class</span> <span class="token class-name">A</span> <span class="token punctuation">{</span>
		<span class="token comment">//静态属性</span>
	    <span class="token keyword">static</span> sex <span class="token operator">=</span> <span class="token string">'male'</span><span class="token punctuation">;</span>

	    <span class="token function">constructor</span> <span class="token punctuation">(</span>name<span class="token punctuation">,</span> job<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
	        <span class="token keyword">this</span><span class="token punctuation">.</span>job <span class="token operator">=</span> job <span class="token operator">||</span> <span class="token string">'unkown'</span><span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
	    
	    <span class="token function">sayName</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
		
		<span class="token comment">//静态方法</span>
	    <span class="token keyword">static</span> <span class="token function">sayHeight</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	        <span class="token keyword">return</span> <span class="token string">'188cm'</span><span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
	    
	    <span class="token comment">//getter</span>
	    <span class="token keyword">get</span> <span class="token function">myJob</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>job<span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
	    
	    <span class="token comment">//setter</span>
	    <span class="token keyword">set</span> <span class="token function">myJob</span> <span class="token punctuation">(</span>job<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	        <span class="token keyword">this</span><span class="token punctuation">.</span>job <span class="token operator">=</span> job<span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
		
		<span class="token comment">//可计算成员名称</span>
	    <span class="token punctuation">[</span>funStr<span class="token punctuation">]</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	        <span class="token keyword">return</span> <span class="token number">18</span><span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
	<span class="token punctuation">}</span>


	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">A</span><span class="token punctuation">.</span>sex<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 'male'</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">A</span><span class="token punctuation">.</span><span class="token function">sayHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '188cm'</span>

	<span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">A</span><span class="token punctuation">(</span><span class="token string">'Tom'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Tom'</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>myJob<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'unkown'</span>
	a<span class="token punctuation">.</span>myJob <span class="token operator">=</span> <span class="token string">'teacher'</span><span class="token punctuation">;</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>myJob<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'teacher'</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '18'</span>

</code></pre><p>继承：</p><pre class="language-js"><code>	<span class="token keyword">class</span> <span class="token class-name">B</span> <span class="token keyword">extends</span> <span class="token class-name">A</span> <span class="token punctuation">{</span>
	    <span class="token keyword">static</span> sex <span class="token operator">=</span> <span class="token string">'female'</span><span class="token punctuation">;</span>
	    <span class="token function">constructor</span> <span class="token punctuation">(</span>name<span class="token punctuation">,</span> job<span class="token punctuation">,</span> married<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	        <span class="token keyword">super</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> job<span class="token punctuation">)</span><span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
	    
	    <span class="token function">sayName</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	        <span class="token keyword">return</span> <span class="token string">'Hi, I\'m '</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
	<span class="token punctuation">}</span>

	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">B</span><span class="token punctuation">.</span>sex<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 'female'</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">B</span><span class="token punctuation">.</span><span class="token function">sayHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '188cm'</span>

	<span class="token keyword">let</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">B</span><span class="token punctuation">(</span><span class="token string">'Lucy'</span><span class="token punctuation">,</span> <span class="token string">'nurse'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Hi, I'm Lucy'</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span>myJob<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'nurse'</span>
	b<span class="token punctuation">.</span>myJob <span class="token operator">=</span> <span class="token string">'teacher'</span><span class="token punctuation">;</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span>myJob<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'teacher'</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '18'</span>

</code></pre><p>参考
<a href="https://mp.weixin.qq.com/s?__biz=MzI4MDYyNjQ1OA==&mid=2247483956&idx=1&sn=adc1ec7ae4cd3f01728fdcb43a38690f&chksm=ebb4d641dcc35f57cd2bf66ba9819874cf300108884700bf3d6a1410c18c0ed4ff3d90d10a8f#rd" target="_blank" rel="noopener noreferrer">1</a><a href="https://juejin.im/post/5b3f23066fb9a04fe820cdbe" target="_blank" rel="noopener noreferrer">2</a></p><h2 id="object的静态方法"><a href="#object的静态方法" aria-hidden="true" class="header-anchor">#</a> Object的静态方法</h2><ul><li><strong>Object.is()</strong> - 比较两个对象是否相同，值类型对象比较值是否相等，引用类型对象比较内存地址是否相同。</li><li><strong>Object.keys()</strong> - 返回对象的可枚举属性数组。</li><li><strong>Object.assign()</strong> - 将多个对象的可枚举属性拷贝到目标对象上，并且返回赋值后的目标对象</li><li><strong>Object.create()</strong> - 获取一个类的原型（prototype）对象，返回值为参数类的原型对象。可用于类继承。</li><li><strong>Object.defineProperties()</strong> - 给对象定义属性，如果存在该属性，则用新定义的属性更新已存在的属性，如果不存在该属性，则添加该属性。</li><li><strong>Object.defineProperty()</strong> - 在对象上定义新属性，或修改对象现有属性，并返回该对象。</li><li><strong>Object.entries()</strong> - 遍历获取对象上所有可枚举的属性，返回结果是一个二维数组[['key1', 'value1'], ['key2', 'value2'], ...]</li><li><strong>Object.freeze()</strong> - 将一个对象上的属性冻结，阻止添加、删除、更新属性到该对象及其原型。返回被冻结的对象。</li><li><strong>Object.getOwnPropertyDescriptors()</strong> - 获取一个对象所有的直接属性的描述信息（直接在对象上的属性，而不是原型链上的属性）。</li><li><strong>Object.getOwnPropertyDescriptor()</strong> - 获取一个对象指定名称的直接属性的描述信息（直接在对象上的属性，而不是原型链上的属性），存在则返回该属性的描述信息，不存在则返回undefined。</li><li><strong>Object.getOwnPropertyNames()</strong> - 获取一个对象所有的直接属性的属性名称（直接在对象上的属性，而不是原型链上的属性）。返回属性名称字符串数组。</li><li><strong>Object.getOwnPropertySymbols()</strong> - 获取对象上所有的Symbol类型的属性列表。</li><li><strong>Object.getPrototypeOf()</strong> - 获取一个对象上的原型对象，其功能和 obj._proto_等同。</li></ul><p>参考
<a href="https://blog.csdn.net/qbian/article/details/79360972" target="_blank" rel="noopener noreferrer">1</a></p><h2 id="手写-promise"><a href="#手写-promise" aria-hidden="true" class="header-anchor">#</a> 手写 promise</h2><p><a href="/blog/vuepress/study/diy.html#promise">这里</a></p><h2 id="promise-异常捕获"><a href="#promise-异常捕获" aria-hidden="true" class="header-anchor">#</a> promise 异常捕获</h2><p>一般情况下，当Promise调用链中抛出一个异常时，异常信息会沿着链路向后传递，直至被捕获。</p><p><strong>Q1</strong>: 在promise调用链中如何在某个then中捕获到异常，但是不停止后面then的执行？</p><p>若链路中也对异常进行了捕获，则后续的函数可能会继续执行，——</p><pre class="language-js"><code><span class="token comment">//then添加第二个参数捕获异常，不影响后面then的执行</span>
Promise<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[onFulfilled_1]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">throw</span> <span class="token string">'throw on onFulfilled_1'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[onFulfilled_2]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> err <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>     <span class="token comment">// 捕获异常</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[onRejected_2]'</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>   <span class="token comment">// 该函数将被调用</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[onFulfilled_3]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token keyword">catch</span><span class="token punctuation">(</span>err <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[catch]'</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//[onFulfilled_1]</span>
<span class="token comment">//[onRejected_2] throw on onFulfilled_1</span>
<span class="token comment">//[onFulfilled_3]</span>
</code></pre><p><strong>Q2</strong>: 在promise调用链中如何在某个then中捕获到异常，立即停止后面then的执行？</p><p>Promise.prototype.then(onFulfilled, onRejected) 若onFulfilled或onRejected是一个函数，当函数返回一个新Promise对象时，原Promise对象的状态将跟新对象保持一致，详见Promises/A+标准。</p><p>因此，当新对象保持“pending”状态时，原Promise链将会中止执行。</p><pre class="language-js"><code>Promise<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[onFulfilled_1]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 返回“pending”状态的Promise对象</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>                   <span class="token comment">// 后续的函数不会被调用</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[onFulfilled_2]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token keyword">catch</span><span class="token punctuation">(</span>err <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[catch]'</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//[onFulfilled_1]</span>
</code></pre><p><strong>Q3</strong>: 为了防止有漏掉的 Promise 异常，建议在全局增加一个对 unhandledrejection 的监听，用来全局监听Uncaught Promise Error。使用方式：</p><pre class="language-js"><code>window<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'unhandledrejection'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>然而自己在浏览器上常识并不好使。。。。</p><h2 id="symbol"><a href="#symbol" aria-hidden="true" class="header-anchor">#</a> Symbol</h2><h3 id="可用来实现私有变量"><a href="#可用来实现私有变量" aria-hidden="true" class="header-anchor">#</a> 可用来实现私有变量</h3><pre class="language-js"><code><span class="token comment">// 定义symbol</span>
<span class="token keyword">const</span> _x <span class="token operator">=</span> <span class="token function">Symbol</span><span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>

<span class="token keyword">class</span> <span class="token class-name">A</span> <span class="token punctuation">{</span>
    <span class="token function">constructor</span> <span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 利用symbol声明私有变量</span>
        <span class="token keyword">this</span><span class="token punctuation">[</span>_x<span class="token punctuation">]</span> <span class="token operator">=</span> x
    <span class="token punctuation">}</span>
    <span class="token function">showX</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">[</span>_x<span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">A</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 自行定义一个相同的Symbol</span>
<span class="token keyword">const</span> x <span class="token operator">=</span> <span class="token function">Symbol</span><span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>
<span class="token comment">// 无法访问</span>
a<span class="token punctuation">[</span>x<span class="token punctuation">]</span>        <span class="token comment">// undefined</span>
<span class="token comment">// 可以访问</span>
a<span class="token punctuation">.</span><span class="token function">showX</span><span class="token punctuation">(</span><span class="token punctuation">)</span>   <span class="token comment">//1</span>
</code></pre><p>参考
<a href="https://juejin.im/post/5c25faf3f265da61380f4b17" target="_blank" rel="noopener noreferrer">1</a></p><h3 id="symbol-iterator"><a href="#symbol-iterator" aria-hidden="true" class="header-anchor">#</a> Symbol.iterator</h3><p>如果对象具有 Symbol.iterator 属性，则表示它是可迭代的。在 ES2015 中，字符串和集合对象（如Set, Map 和 Array）带有Symbol.iterator 属性，因此是可迭代的。</p><p>如
<img src="https://user-gold-cdn.xitu.io/2019/1/21/16870f2e5b38aef9?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt></p><p>普通对象不可迭代，但如果在其上定义了 Symbol.iterator 属性，则它可变为可迭代对象</p><p>如
<img src="https://user-gold-cdn.xitu.io/2019/1/21/16870f2e64eb4179?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt></p><p>以上过程可以使用生成器（ generator ）函数进行大大简化：
<img src="https://user-gold-cdn.xitu.io/2019/1/21/16870f2e706ed08f?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt></p><h2 id="proxy-reflect"><a href="#proxy-reflect" aria-hidden="true" class="header-anchor">#</a> Proxy/Reflect</h2><p>Proxy 拦截对象行为</p><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Proxy</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token keyword">get</span><span class="token punctuation">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token string">`getting </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>key<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">!`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> Reflect<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token keyword">set</span><span class="token punctuation">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token string">`setting </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>key<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">!`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> Reflect<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><h3 id="reflect"><a href="#reflect" aria-hidden="true" class="header-anchor">#</a> Reflect</h3><ul><li>为操作对象而提供的新API</li><li>不是构造函数，只这么调用 Reflect['method']</li><li>Proxy 有的方法，Reflect都有</li><li>举例：
<ul><li>'a' in obj --&gt; Reflect.has(obj, 'a);</li><li>delete obj.a --&gt; Reflect.deleteProperty(obj, 'a');</li><li>Object.defineProperty(target, property, attributes) 失败会报错 --&gt; Reflect.defineProperty(target, property, attributes) 返回true/false</li><li>等等</li></ul></li></ul><p>参考
<a href="https://juejin.im/post/5b62e19cf265da0fa1224248" target="_blank" rel="noopener noreferrer">1</a></p><h2 id="async-await"><a href="#async-await" aria-hidden="true" class="header-anchor">#</a> async/await</h2><p>特点：</p><ul><li>async 函数是 Generators和Promises特性功能的语法糖</li><li>建立在 promise 之上。它会声明一个异步函数，并隐式地返回一个Promise。因此可以直接return变量，无需使用 Promise.resolve 进行转换。</li><li>和 promise 一样，是非阻塞的。但不用写 then 及其回调函数，这减少代码行数，也避免了代码嵌套。而且，所有异步调用，可以写在同一个代码块中，无需定义多余的中间变量。</li><li>它的最大价值在于，可以使异步代码在形式上更接近于同步代码。</li><li>与 await 一起使用，await 只能在 async 函数体内。</li><li>await 是个运算符，用于组成表达式，它会阻塞后面的代码。如果等到的是 Promise 对象，则得到其 resolve 值。否则，会得到一个表达式的运算结果。</li></ul><p>举例：某业务，每一个步骤都需要之前每个步骤的结果。promise 写法：</p><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">doIt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token string">&quot;doIt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">const</span> time1 <span class="token operator">=</span> <span class="token number">300</span><span class="token punctuation">;</span>
    <span class="token function">step1</span><span class="token punctuation">(</span>time1<span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>time2 <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token function">step2</span><span class="token punctuation">(</span>time1<span class="token punctuation">,</span> time2<span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>time3 <span class="token operator">=&gt;</span> <span class="token punctuation">[</span>time1<span class="token punctuation">,</span> time2<span class="token punctuation">,</span> time3<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>times <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">const</span> <span class="token punctuation">[</span>time1<span class="token punctuation">,</span> time2<span class="token punctuation">,</span> time3<span class="token punctuation">]</span> <span class="token operator">=</span> times<span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token function">step3</span><span class="token punctuation">(</span>time1<span class="token punctuation">,</span> time2<span class="token punctuation">,</span> time3<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>result <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token string">`result is </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>result<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">doIt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>async/await 写法：</p><pre class="language-js"><code><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">doIt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token string">&quot;doIt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">const</span> time1 <span class="token operator">=</span> <span class="token number">300</span><span class="token punctuation">;</span>
    <span class="token keyword">const</span> time2 <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">step1</span><span class="token punctuation">(</span>time1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">const</span> time3 <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">step2</span><span class="token punctuation">(</span>time1<span class="token punctuation">,</span> time2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">const</span> result <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">step3</span><span class="token punctuation">(</span>time1<span class="token punctuation">,</span> time2<span class="token punctuation">,</span> time3<span class="token punctuation">)</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token string">`result is </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>result<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">doIt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>await 在等什么</p><ul><li>如果它等到的不是一个 Promise 对象，那 await 表达式的运算结果就是它等到的东西。</li><li>如果它等到的是一个 Promise 对象，它会阻塞后面的代码，等着 Promise 对象 resolve，然后得到 resolve 的值，作为 await 表达式的运算结果。</li></ul><p>参考
<a href="https://segmentfault.com/a/1190000007535316" target="_blank" rel="noopener noreferrer">1</a><a href="https://juejin.im/post/5c39523651882525a67c53d6" target="_blank" rel="noopener noreferrer">2</a><a href="https://juejin.im/post/5c45dffef265da61163a13e2" target="_blank" rel="noopener noreferrer">3</a></p></div><div class="wx-wrapper" data-v-0de8a4bf><p class="wx-txt" data-v-0de8a4bf>欢迎关注</p><img src="http://pn4meizzc.bkt.clouddn.com/wxqr.png" class="wx-img" data-v-0de8a4bf></div><div class="beg-wrapper" data-v-1f1af99c><a href="javascript:;" class="btn show" data-v-1f1af99c><img src="http://pn4meizzc.bkt.clouddn.com/blogshang_log.png" data-v-1f1af99c></a><p class="shang-txt" data-v-1f1af99c>赏不赏，看您心情</p><img src="http://pn4meizzc.bkt.clouddn.com/blogWechatIMG35.jpeg" class="shangImg" data-v-1f1af99c></div><!----><div class="content page-nav"><p class="inner"><span class="prev">
        ← <a href="/blog/vuepress/study/webpack.html" class="prev">
          webpack
        </a></span><span class="next"><a href="/blog/vuepress/study/questions.html">
          问题
        </a> →
      </span></p></div></div></div></div>
    <script src="/blog/vuepress/assets/js/11.4d115f98.js" defer></script><script src="/blog/vuepress/assets/js/app.15770e34.js" defer></script>
  </body>
</html>
